<!--- ==================================================
NAME:	crt_order.cfm
DESC:	This is the file that creates an order.
=================================================== --->
<cfsetting enablecfoutputonly="yes">

<!--- default data --->
<cfparam name="arguments.url" default="#application.xtiurl#">
<cfparam name="arguments.dtd" default="#application.xtidtd#">
<cfparam name="arguments.debug" default="false">
<cfparam name="arguments.debuglevel" default="1,2,3">
<cfparam name="arguments.customerid">
<cfparam name="arguments.shipto_customerid">
<cfparam name="arguments.contactid" default="">
<cfparam name="arguments.crt_order" default="">
<cfparam name="arguments.returnvar" default="neworder">
<cfparam name="arguments.source">
<cfparam name="arguments.ship_complete" default="">
<cfparam name="arguments.tsr_taker">
<cfparam name="arguments.order_type" default="R">
<cfparam name="arguments.locid" default="">
<cfparam name="arguments.cid" default="">
<cfparam name="arguments.currency" default="USD">
<cfparam name="arguments.perform_avs" type="numeric" default="0">
	<cfset arguments.perform_avs="#application.string_validation.only_numeric(arguments.perform_avs)#">
	
<cfif arguments.perform_avs IS NOT "1">
	<cfset arguments.perform_avs = 0>
</cfif>

<cfparam name="arguments.payment_type" default="">
<cfswitch expression="#LCase(arguments.payment_type)#">
	<cfcase value="v">
		<cfset arguments.payment_type = "VISA">
	</cfcase>
	<cfcase value="m">
		<cfset arguments.payment_type = "MC">
	</cfcase>
	<cfcase value="d">
		<cfset arguments.payment_type = "DC">
	</cfcase>
	<cfdefaultcase>
		<cfset arguments.payment_type = "">
	</cfdefaultcase>
</cfswitch>

<cfparam name="arguments.pay_type" default="V">

<cfparam name="arguments.array_ccs_new" default="">
<cfparam name="po_num" default="">
<cfif po_num is not "">
<cfparam name="arguments.po_number" default="#po_num#">
<cfelse>
	<cfparam name="arguments.po_number" default="">
</cfif>
<cfparam name="arguments.last_four_ssn" type="any" default="">
<cfparam name="arguments.date_of_birth" type="any" default="">
<cfparam name="arguments.promotional_financing" type="any" default="">
<cfparam name="arguments.order_origin" type="any" default="WEB">
<cfparam name="arguments.company_name">
<cfparam name="arguments.attention">
<cfparam name="arguments.address1">
<cfparam name="arguments.address2" default="">
<cfparam name="arguments.address3" default="">
<cfparam name="arguments.address4" default="">
<cfparam name="arguments.city">
<cfparam name="arguments.state">
<cfparam name="arguments.country">
<cfparam name="arguments.zip" default="">
<cfparam name="arguments.geo_code" default="">
<cfparam name="arguments.phone" default="">
<cfparam name="arguments.code">
<cfparam name="arguments.account_number" default="">
<cfparam name="arguments.ship_price">
<cfparam name="arguments.ship_code_reason" default="">
<cfparam name="arguments.packing_slip_notes" default="">
<cfparam name="arguments.invoice_notes" default="">
<cfparam name="arguments.management_notes" default="">
<cfparam name="arguments.auto_bo" default="">
<cfparam name="arguments.future_ship" default="">
<cfparam name="arguments.array_parts">
<cfparam name="arguments.array_systems" default="">
<cfparam name="arguments.validate_po" default="">

<!---
	<!--- include the encryption module to for encrypting/decrypting credit card data
		Functions included: 
			• sap_encrypt(originalData) == returns the encrypted string data
			• sap_decrypt(encryptedData) == returns the decrypted string data (original data)
	---><cfinclude template="sap_encryption.cfm">
--->

<!--- Defect 1821 - Address Verification is required for all create and change order posts --->
<cfquery name="verify_address" datasource="#application.dsn#">
	asp_invoice_number_get_by_customer_and_address
		@iCustomer_Number = #arguments.customerid#,
		@iAddress_1 = '#trim(arguments.address1)#',
		@iAddress_2 = <cfif len(trim(arguments.address2))>'#arguments.address2#'<cfelse>null</cfif>,
		@iCity = '#trim(arguments.city)#',
		@iState_Code = '#trim(arguments.state)#',
		@iPostal_Code = #arguments.zip#
</cfquery>
<cfif verify_address.recordcount gte 1>
	<cfset arguments.address_verified = "Y">
<cfelse>
	<cfset arguments.address_verified = "N">
</cfif>


<!--- build xml statement --->
<cfscript>
new_payment="";
if  (isarray(arguments.array_ccs_new) and arraylen(arguments.array_ccs_new) GT 0) {
	indexcnt_new = arraylen(arguments.array_ccs_new) + 1;
	for ( j=1; j lt indexcnt_new ; j=j+1) {
		
		var.sap_encryption_util = createObject("java","UtilEncrypt");
		var.sap_encryption_key = application.sap_encryption_key;
		
		var.sap_encryption_originalData_ccnum = arguments.array_ccs_new[j].cc_num;		
		var.card_type = application.string_validation.getCardType(var.sap_encryption_originalData_ccnum);		
		//if the cc_type was passed, then set it
		if (arguments.array_ccs_new[j].cc_type NEQ "") {
			arguments.payment_type = arguments.array_ccs_new[j].cc_type;
		}
		//else if find out what kind of card type it is
		else if(IsStruct(var.card_type) AND StructKeyExists(var.card_type,"card_type_abbr") AND Len(var.card_type.card_type_abbr) GT 0){
			arguments.payment_type = var.card_type.card_type_abbr; }
		//else default it
		else {
			// do not replace arguments.payment_type variable when no card type could be determined for this card entry.			
			if(Len(arguments.pay_type) GT 0){
				arguments.payment_type = arguments.pay_type;
			}
		}
		
		var.sap_encryption_encodeEncryptData_ccnum = var.sap_encryption_util.encrypt( var.sap_encryption_key, var.sap_encryption_originalData_ccnum, "");
		
		var.sap_encryption_originalData_sec = arguments.array_ccs_new[j].cc_security;
		var.sap_encryption_encodeEncryptData_sec = var.sap_encryption_util.encrypt( var.sap_encryption_key, var.sap_encryption_originalData_sec, "");
		
		new_payment = new_payment & '
		<payment type="#arguments.payment_type#">
			<card_number>#var.sap_encryption_encodeEncryptData_ccnum#</card_number>
			<expr_date>#ucase(arguments.array_ccs_new[j].cc_exp)#</expr_date>
			<amount>#Replace(arguments.array_ccs_new[j].cc_amt, ',', '', 'ALL')#</amount>
			<security_code>#var.sap_encryption_encodeEncryptData_sec#</security_code>
			<last_four_ssn>#arguments.last_four_ssn#</last_four_ssn>
			<date_of_birth>#arguments.date_of_birth#</date_of_birth>
			<promotional_financing>#arguments.promotional_financing#</promotional_financing>
		</payment>			
		';
	}
}

cart = "";
subtotal = 0;
// Start with standalone parts
if  (isarray(arguments.array_parts) and arraylen(arguments.array_parts) GT 0) {
	indexcnt = arraylen(arguments.array_parts) + 1;
	for ( i=1; i lt indexcnt ; i=i+1) {
		cart = cart & '
			<line_item>
				<product>
					<product_code>#ucase(arguments.array_parts[i].product)#</product_code>
					<quantity>#ucase(arguments.array_parts[i].quantity)#</quantity>
					<price>#ucase(arguments.array_parts[i].price)#</price>
					<promo_code_1>#ucase(arguments.array_parts[i].promo_code_1)#</promo_code_1>
					<promo_code_2>#ucase(arguments.array_parts[i].promo_code_2)#</promo_code_2>
					<rsn_code_prc>#ucase(arguments.array_parts[i].price_reason_code)#</rsn_code_prc>
					<weight>#application.string_validation.YNFormat(arguments.array_parts[i].weight)#</weight>
					<condition_type>#arguments.array_parts[i].condition_type#</condition_type>
					<item_number>#i#</item_number>
				</product>
			</line_item>
		';
	}
}

// Now the systems 
if  (isarray(arguments.array_systems) and arraylen(arguments.array_systems)) {
	indexcnt1 = arraylen(arguments.array_systems) + 1;
	for (x=1; x lt indexcnt1; x=x+1) {
		cart = cart & '
			<line_item>
				<configuration>
					<quantity>#ucase(arguments.array_systems[x].quantity)#</quantity>
					<baseunit>
						<product_code>#ucase(arguments.array_systems[x].b_product)#</product_code>
						<price>#ucase(arguments.array_systems[x].b_price)#</price>
						<promo_code_1>#ucase(arguments.array_parts[i].promo_code_1)#</promo_code_1>
						<promo_code_2>#ucase(arguments.array_parts[i].promo_code_2)#</promo_code_2>
						<rsn_code_prc>#ucase(arguments.array_systems[x].price_reason_code)#</rsn_code_prc>
					</baseunit>';
		indexcnt2 = arraylen(arguments.array_systems[x].subitem) + 1;
	for (y=1; y lt indexcnt2;  y=y+1) {
		cart = cart & '
					<component>
						<product_code>#ucase(arguments.array_systems[x].subitem[y].product)#</product_code>
						<quantity>#ucase(arguments.array_systems[x].subitem[y].quantity)#</quantity>
						<price>#ucase(arguments.array_systems[x].subitem[y].price)#</price>
						<rsn_code_prc>#ucase(arguments.array_systems[x].subitem[y].price_reason_code)#</rsn_code_prc>
					</component>';
		}
		cart = cart & '
				</configuration>
			</line_item>';
	}
}
// Now build the rest of the xml to post to middle ware. KEEP FORMATING SAME AS BELOW...
xi_namespace='#trim(application.xi_namespace)#:ORDERS:CreateOrder';
xi_interface='CreateOrder_OB';
xml = '<xtiinput>
	<inputheader>
		<actionset>
			<action>crt_order</action>
		</actionset>
	</inputheader>
	<inputdata>
		<system>#variables.system#</system>
		<customerid>#ucase(arguments.customerid)#</customerid>
		<shipto_customerid>#ucase(arguments.shipto_customerid)#</shipto_customerid>
		<contactid>#ucase(arguments.contactid)#</contactid>
		<perform_avs>#arguments.perform_avs#</perform_avs>
		<order_header>
			<source>#ucase(arguments.source)#</source>
			<ship_complete>#ucase(arguments.ship_complete)#</ship_complete>
			<tsr_taker>#arguments.tsr_taker#</tsr_taker>
			<order_type>#xmlformat(ucase(arguments.order_type))#</order_type>
			<pay_type>#arguments.pay_type#</pay_type>
			<locid>#arguments.locid#</locid>
			<cid>#arguments.cid#</cid>
			<future_ship_date>#ucase(arguments.future_ship)#</future_ship_date>
			<shipment_type>#ucase(arguments.auto_bo)#</shipment_type>
			<currency>#trim(arguments.currency)#</currency>
			<po_number>#ucase(arguments.ponumber)#</po_number>
		</order_header>
		#new_payment# 
		<shipping_addr>
			<company_name>#xmlformat(ucase(arguments.company_name))#</company_name>
			<attention>#xmlformat(ucase(arguments.attention))#</attention>
			<address_1>#xmlformat(ucase(arguments.address1))#</address_1>
			<address_2>#xmlformat(ucase(arguments.address2))#</address_2>
			<city>#ucase(arguments.city)#</city>
			<state>#ucase(arguments.state)#</state>
			<country>#ucase(arguments.country)#</country>
			<zip>#ucase(arguments.zip)#</zip>
			<geo_code>#ucase(arguments.geo_code)#</geo_code>
			<phone>#ucase(arguments.phone)#</phone>
			<addr_verified>#arguments.address_verified#</addr_verified>
		</shipping_addr>
		<shipping_codes>
			<method>
				<code>#ucase(arguments.code)#</code>
				<account_number>#ucase(arguments.account_number)#</account_number>
				<price>#ucase(arguments.ship_price)#</price>
				<rsn_code_ship>#ucase(arguments.ship_code_reason)#</rsn_code_ship>
			</method>
		</shipping_codes>
		<system_notes>
			<packing_slip_notes>#ucase(arguments.packing_slip_notes)#</packing_slip_notes>
			<invoice_notes>#ucase(arguments.invoice_notes)#</invoice_notes>
			<management_notes>#ucase(arguments.management_notes)#</management_notes>
		</system_notes>
		<shopping_cart>
			#cart#
		</shopping_cart>
		<validate_po>#ucase(arguments.validate_po)#</validate_po>
	</inputdata>
</xtiinput>';
</cfscript>